// pages/myshijuan/myshijuan.js
var app = getApp();
var ea = require('../../utils/each.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    studentid:0,
    page:1,
    shuju:[],
    teacher:[],
    indexs:[],
    getstudents_papers: app.data.url +'/zhongxue/maoapi/getstudents_papers',
    delete_students_papers: app.data.url + '/zhongxue/maoapi/delete_students_papers',
    fasong_papers: app.data.url + '/zhongxue/maoapi/fasong_papers',

    index:0,
    shangchuan:app.data.url +"/uploads/zhongxue/shangchuanshijuans.png",
    xiala:app.data.url +"/uploads/zhongxue/xiala.png",
    delet:app.data.url +"/uploads/zhongxue/delete.png",
    hongdian: app.data.url +"/uploads/zhongxue/hongdian.png",
    huihua:app.data.url +"/uploads/zhongxue/huihua.png",
    arrays: [
      { images: [app.data.url +"/uploads/zhongxue/shijuans.png", app.data.url +"/uploads/zhongxue/shijuans2.png", app.data.url +"/uploads/zhongxue/shijuans2.png"], kemu: "语文", name: "四年级语文期中考试卷精选", create_time: "2018-16-17" },
      { images: [app.data.url +"/uploads/zhongxue/shijuans.png", app.data.url +"/uploads/zhongxue/shijuans2.png", app.data.url +"/uploads/zhongxue/shijuans2.png"], kemu: "数学", name: "四年级语文期中考试卷精选", create_time: "2018-16-17" },
      { images: [app.data.url +"/uploads/zhongxue/shijuans.png", app.data.url +"/uploads/zhongxue/shijuans2.png", app.data.url +"/uploads/zhongxue/shijuans2.png"], kemu: "英语", name: "四年级语文期中考试卷精选", create_time: "2018-16-17" },
    ],
    array:["张雨轩老师","侯华明老师"],
  },
  //试卷评价 
  topingjia:function(e){
    var id = e.currentTarget.dataset.id;
  wx.navigateTo({
    url: '/pages/sjpingjia/sjpingjia?id='+id,
  })
  },
  //试卷上传
  shangchuan:function(){
    var studentid = this.data.studentid;
    wx.navigateTo({
      url: '/pages/sjshangchuan/sjshangchuan?id=' + studentid,
    })
  },
  

  //点击试卷放大
  imge:function(e){

    var index = e.currentTarget.dataset.index;
    console.log(index);
 
    wx.previewImage({
      urls: this.data.shuju[index].testpaper
    })
  },

  /** 
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.id;
    this.setData({ studentid:id})
    this.getData();
  },
  getData:function(func){
    var user = wx.getStorageSync('user');
    var page = this.data.page;
    if(page==0){
      wx.showToast({
        title: '没有更多了',
        icon:'none',
        duration: app.data.quanbushijian
      })
      return
    }
    var studentid = this.data.studentid;
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    console.log(studentid);
    wx.request({
      url: that.data.getstudents_papers,
      data: {token:user.token,studentid:studentid,page:page},
      header: {},
      method: 'POST',
      dataType: 'json',
      responseType: 'text',
      success: function(res) {
        var data = res.data;
        console.log(data);
        // return;
        if(data.code==200){
          var newshuju = data.data;
          // console.log(newshuju);
          // var teacher = data.data.teacher;
          // console.log(teacher);
          // that.setData({teacher:teacher});
          var shuju = that.data.shuju;
          var indexs = that.data.indexs;
          if(newshuju.length<10){
            that.setData({page:0})
          }else{
            that.setData({page:page+1})
          }
          ea.each(newshuju,function(i,v){
            indexs.push(0);
          })
          // console.log(indexs);
          that.setData({indexs:indexs});
          shuju = ea.addArray(shuju,newshuju);
          console.log(shuju);
          that.setData({shuju:shuju});
          if(func){
            func();
          }
        }else{
          wx.showToast({
            title: data.msg,
            icon:'none',
            duration: app.data.quanbushijian
          })
        }
      },
      fail: function(res) {},
      complete: function(res) {
        wx.hideLoading()
      },
    })
  },
  xianshi:function(e){
    console.log(e);
    var id = e.currentTarget.dataset.id;
  wx.downloadFile({
    url:app.data.url+'/zhongxue/pdfs/index?id='+id,
    success:function(res){
      console.log(res);
      var tempFilePath = res.tempFilePath;
      wx.openDocument({
        filePath: tempFilePath,
        success:function(){
          console.log('查看成功');
        }
      })
    }
  })
  // wx.request({
  //   url: app.data.url + '/zhongxue/pdfs/index',
  //   method:'POST',
  //   data:{id:id},
  //   success:function(res){
  //     console.log(res);
  //   }
  // })
  },
  delete:function(e){
    var id = e.currentTarget.dataset.id;
    var user = wx.getStorageSync('user');
    var that = this;
    wx.showModal({
      title: '提示',
      content: '你确定要删除这个试卷吗？',
      showCancel: true,
      success: function(res) {
        if (res.confirm){
          wx.showLoading({
            title: '删除中',
          })
          wx.request({
            url: that.data.delete_students_papers,
            data: {token:user.token,id:id},
            header: {},
            method: 'POST',
            dataType: 'json',
            responseType: 'text',
            success: function(res) {
              var data = res.data;
              if(data.code==200){
                wx.showToast({
                  title: '删除成功',
                  duration: app.data.quanbushijian
                });
                that.setData({ shuju: [], page: 1, indexs:[]});
                that.getData();
              }else{
                wx.showToast({
                  title: data.msg,
                  icon:'none',
                  duration: app.data.quanbushijian
                })
              }
            },
            fail: function(res) {},
            complete: function(res) {
              wx.hideLoading()
            },
          })
        }
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  bindPickerChange:function(e){
    // console.log(e)
    // var value = e.detail.value;
    // var index = e.currentTarget.dataset.index;
    // console.log(index);
    // console.log(value);
    var inde = parseInt(e.detail.value);
    // console.log(inde);
    var index = e.currentTarget.dataset.index;
    console.log(index);
    var indexs = this.data.indexs;
    // console.log(indexs);
    indexs[index] = inde;
    console.log(indexs);
    this.setData({ indexs:indexs});
  },
  fasongshijuan:function(e){
    // console.log(e);
    var papersid = e.currentTarget.dataset.id;
    var guawazhi = e.currentTarget.dataset.guawazhi;
    var teacher = e.currentTarget.dataset.baoming;
    // console.log(teacher);
    if(teacher==0){
    wx.showToast({
      title: '亲，目前需要在校区确认学位就学才能让名师评价哦！',
      icon:'none',
      duration: app.data.quanbushijian
    })
    return
    }
    if (guawazhi == 0) {
      wx.showModal({
        title: '提示',
        content: '亲，您还没有到学校确定学位哦,暂时不能使该功能！',
        showCancel: false
      })
      return
    }
    var indexs = this.data.indexs;
    var index = e.currentTarget.dataset.index
    console.log(indexs);
    console.log(index);
  
    var shuju = this.data.shuju;
    var teacherid = shuju[index].teacher[indexs[index]].id;
    var user = wx.getStorageSync('user');
    var that = this;
    // console.log(teacherid);
    // return
    // console.log(user.token);
    // console.log(papersid);
    // console.log(teacherid);
    // console.log(papersid);
    // console.log(teacherid);
    // return
    wx.showModal({
      title: '提示',
      content: '你确定发送给这个老师吗？',
      showCancel:true,
      success:function(es){
        if (es.confirm){
          wx.showLoading({
            title: '发送中',
          })
          wx.request({
            url: that.data.fasong_papers,
            data: { token: user.token, papersid: papersid, teacherid: teacherid},
            header: {},
            method: 'POST',
            dataType: 'json',
            responseType: 'text',
            success: function(res) {
              console.log(res);
              // return
              var data = res.data;
              console.log(data);
              if(data.code==200){
                wx.showToast({
                  title: data.msg,
                  icon:'none',
                  duration: app.data.quanbushijian
                })
                setTimeout(function(){
                  that.onPullDownRefresh();
                },1500)
              }else{
                wx.showToast({
                  title: data.msg,
                  icon:'none',
                  duration: app.data.quanbushijian
                })
              }
            },
            fail: function(res) {},
            complete: function(res) {
              wx.hideLoading()
            },
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({ page: 1, shuju: [], indexs:[]});
    this.getData(function(){
      wx.stopPullDownRefresh()
    })
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getData();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      path: '/pages/indexfirst/indexfirst',
    }
  }
})